package com.scheduler.backend.config;

import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.ExternalDocumentation;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.headers.Header;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import io.swagger.v3.oas.models.media.StringSchema;
import io.swagger.v3.oas.models.parameters.Parameter;
import io.swagger.v3.oas.models.security.SecurityScheme;
import io.swagger.v3.oas.models.security.SecurityScheme.In;
import org.springdoc.core.Constants;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringBootVersion;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * Springdoc-openapi配置
 */

@Configuration
@ConditionalOnProperty(name = Constants.SPRINGDOC_ENABLED, havingValue = "true", matchIfMissing = true)
public class SpringDocConfig implements WebMvcConfigurer, ApplicationListener<ContextRefreshedEvent> {

    /**
     * 项目应用名
     */
    @Value("${application.name}")
    private String applicationName;

    /**
     * 项目版本信息
     */
    @Value("${application.version}")
    private String applicationVersion;

//    /**
//     * API文档访问地址
//     */
//    @Value("${springdoc.swagger-ui.path}")
//    private String apiDocUiPath;
//
//    /**
//     * API文档访问地址
//     */
//    @Value("${springdoc.api-docs.path}")
//    private String apiDocPath;

    /**
     * 项目描述信息
     */
    @Value("${application.description}")
    private String applicationDescription;

    /**
     * 上下文路径
     */
    //  @Value("${server.servlet.context-path}")
//    private String contextPath = "http://localhost";

    /**
     * web访问端口
     */
    // @Value("${server.port}")
//    private int port = 8080;
    @Bean
    public OpenAPI springShopOpenAPI() {
        Components components = new Components();

        // 添加auth认证header
        components.addSecuritySchemes("auth", new SecurityScheme().type(SecurityScheme.Type.APIKEY).name("auth").in(In.HEADER).scheme("basic"));

        // 添加全局header
        components.addParameters("myHeader1", new Parameter().in(In.HEADER.toString()).schema(new StringSchema()).name("myHeader1"));
        components.addHeaders("myHeader2", new Header().description("myHeader2 header").schema(new StringSchema()));

        return new OpenAPI()
                .components(components)
                .info(new Info().title(applicationName + " Api Doc")
                        .description(applicationDescription)
                        .version("Application Version: " + applicationVersion + "\n Spring Boot Version: " + SpringBootVersion.getVersion())
                        .license(new License().name("Apache 2.0").url("http://springdoc.org")))
                .externalDocs(new ExternalDocumentation()
                        .description("SpringShop Wiki Documentation")
                        .url("https://springshop.wiki.github.org/docs"));
    }

    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {

    }
}
